package com.tudouji.project.arealand.service.impl;

import java.util.Date;
import java.util.List;

import com.flow.service.mapper.IFileInfoMapper;
import com.flow.service.pojo.flow.FileInfo;
import com.flow.service.pojo.flow.FlowTempNode;
import com.flow.service.pojo.user.FlowUser;
import com.tudouji.common.utils.IdUtils;
import com.tudouji.common.utils.SecurityUtils;
import com.tudouji.common.utils.StringUtils;
import com.tudouji.framework.web.domain.AjaxResult;
import com.tudouji.project.arealand.domain.Plantmanager;
import com.tudouji.project.arealand.mapper.PlantmanagerMapper;
import com.tudouji.project.system.domain.SysUser;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.arealand.mapper.TArealandMapper;
import com.tudouji.project.arealand.domain.TArealand;
import com.tudouji.project.arealand.service.ITArealandService;

/**
 * 地块Service业务层处理
 * 
 * @author tudouji
 * @date 2023-03-15
 */
@Service
public class TArealandServiceImpl implements ITArealandService 
{
    @Autowired
    private TArealandMapper tArealandMapper;
    @Autowired
    private PlantmanagerMapper plantmanagerMapper;
    @Autowired
    private IFileInfoMapper fileInfoMapper;

    /**
     * 查询地块
     * 
     * @param guid 地块ID
     * @return 地块
     */
    @Override
    public TArealand selectTArealandById(String guid)
    {
        return tArealandMapper.selectTArealandById(guid);
    }

    /**
     * 查询地块列表
     * 
     * @param tArealand 地块
     * @return 地块
     */
    @Override
    public List<TArealand> selectTArealandList(TArealand tArealand)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        String ut = sysUser.getUserType();
        if (StringUtils.equalsIgnoreCase("1", ut)) {
            tArealand.setAUserid(sysUser.getGuid());
        }
        if (StringUtils.equalsIgnoreCase("4", ut)) {
            tArealand.setManagerid(sysUser.getGuid());
        }
        tArealand.setFlag(1);
        return tArealandMapper.selectTArealandList(tArealand);
    }

    /**
     * 新增地块
     * 
     * @param tArealand 地块
     * @return 结果
     */
    @Override
    public int insertTArealand(TArealand tArealand)
    {
        return tArealandMapper.insertTArealand(tArealand);
    }

    /**
     * 修改地块
     * 
     * @param tArealand 地块
     * @return 结果
     */
    @Override
    public int updateTArealand(TArealand tArealand)
    {
        return tArealandMapper.updateTArealand(tArealand);
    }

    /**
     * 批量删除地块
     * 
     * @param guids 需要删除的地块ID
     * @return 结果
     */
    @Override
    public int deleteTArealandByIds(String[] guids)
    {
        return tArealandMapper.deleteTArealandByIds(guids);
    }

    /**
     * 删除地块信息
     * 
     * @param guid 地块ID
     * @return 结果
     */
    @Override
    public int deleteTArealandById(String guid)
    {
        return tArealandMapper.deleteTArealandById(guid);
    }

    @Override
    public AjaxResult saveOrUpdate(TArealand entity, String fileIds) {
        AjaxResult result = AjaxResult.success();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String ut = user.getUserType();
        entity.setFlag(1);
        if (StringUtils.isNotBlank(entity.getGuid())) {
            if (StringUtils.equalsIgnoreCase("1", ut) || StringUtils.equalsIgnoreCase("4", ut)) {
                entity.setManagerid("");
                tArealandMapper.updateTArealand(entity);
            }
        } else {
            entity.setACreatedate(new Date());
            entity.setACreatedate(new Date());
            if (StringUtils.equalsIgnoreCase("1", ut)) {
                entity.setAUserid(user.getGuid());
                entity.setGuid(IdUtils.fastSimpleUUID());
                tArealandMapper.insertTArealand(entity);
            } else if (StringUtils.equalsIgnoreCase("4", ut)) {
                Plantmanager pm = plantmanagerMapper.selectPlantmanagerById(user.getGuid());
                if (pm == null){
                    return AjaxResult.error("管理员为空！");
                }
                entity.setAUserid(pm.getUserid());
                entity.setManagerid(pm.getManagerid());
                entity.setGuid(IdUtils.fastSimpleUUID());
                tArealandMapper.insertTArealand(entity);
            }
        }
        saveFile(fileIds, entity.getGuid());
        return result;
    }

    @Override
    public AjaxResult getAreaLands(String latitude, String longitude) {
        SysUser user = SecurityUtils.getLoginUser().getUser();

        TArealand area = new TArealand();
        if (StringUtils.equalsIgnoreCase("1", user.getUserType())) {
            area.setAUserid(user.getGuid());
        }
        area.setLatitude(latitude);
        area.setLongitude(longitude);
        area.setFlag(1);
        List<TArealand> list = tArealandMapper.juli(area);
        return AjaxResult.success(list);
    }

    @Override
    public AjaxResult getAreaLandsByFarmerId(String farmerid) {
        TArealand land=new TArealand();
        land.setAUserid(farmerid);
        land.setFlag(1);//只获取实体地块
        List<TArealand> list = tArealandMapper.selectTArealandList(land);
        return AjaxResult.success(list);
    }


    public Integer saveFile(String fileIds, String linkedId) {
        Integer result = 0;

        try {
            if (org.apache.commons.lang3.StringUtils.isNotBlank(fileIds)) {
                String[] ids = fileIds.split(",");
                Long[] idss = (Long[])((Long[]) ConvertUtils.convert(ids, Long.class));
                Long[] var8 = idss;
                int var9 = idss.length;

                for(int var10 = 0; var10 < var9; ++var10) {
                    Long id = var8[var10];
                    FileInfo info = new FileInfo();
                    info.setId(id);
                    info.setType("flow_operation");
//                    info.setLinkedId(linkedId);
                    result = this.fileInfoMapper.updateFileInfo(info);
                }
            }

            return result;
        } catch (Exception var16) {
            result = -1;
            return result;
        } finally {
            ;
        }
    }
}
